using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace JointEffort_Gallery
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    //TitleScreen also serves as the main menu.
    public class TitleScreen : Screens
    {
        
        Game g;
        SpriteBatch sb;
        SpriteFont sf;
        SpriteFont titleFont;
        Color colour = Color.Magenta;
        bool change = true;
        // 
        const uint DELAY = 100; // milisecond delay
        float fWaitTime = 0; // counter 

        Texture2D triOSlogo, jointEffort;

        List<string> MenuSelections = new List<string>();

        MouseState mouse, prevMouse;
        KeyboardState keyboard, prevKeyboard;

        public TitleScreen(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
            g = game;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            //base.Initialize();
            triOSlogo = g.Content.Load<Texture2D>(@"images\triOS");
            jointEffort = g.Content.Load<Texture2D>(@"images\JointEffort");
            titleFont = g.Content.Load<SpriteFont>(@"fonts\titleFont");
            MenuSelections.Add("Start Game");
            MenuSelections.Add("Options");
            MenuSelections.Add("Hall Of Fame");
            MenuSelections.Add("Exit");
            keyboard = Keyboard.GetState();
            mouse = Mouse.GetState();
        }

        protected override void LoadContent()
        {
            //base.LoadContent();
            //LoadContent code is never accessed, load all content inside Initialize()
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            keyboard = Keyboard.GetState();
            // TODO: Add your update code here
            if (change == true)
            {
                if ((int)Selection > 0 && keyboard.IsKeyDown(Keys.Up)/* && change == true*/)
                {
                    Selection--;
                    change = false;
                    fWaitTime = DELAY; 
                }
                if ((int)Selection < MenuSelections.Count() - 1 && keyboard.IsKeyDown(Keys.Down) /*&& change == true*/)
                {
                    change = false;
                    fWaitTime = DELAY; 
                    Selection++; 
                }
            }
            
        }
        
        public override void Draw(GameTime gameTime)
        {
            
            sb = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            sf = Game.Services.GetService(typeof(SpriteFont)) as SpriteFont;
            
            sb.Begin();
            for (int i = 0; i < MenuSelections.Count; i++)
            {
                colour = (i == (int)Selection) ? Color.Yellow : Color.Black;
                sb.DrawString(sf, MenuSelections[i], new Vector2(TrueMiddle.X - (sf.MeasureString(MenuSelections[i]).X/2), TrueMiddle.Y - (sf.LineSpacing*MenuSelections.Count) + (sf.LineSpacing*i)), colour);
            }

            // tack on a delay so that things dont happen at the FPS speed 
            if (fWaitTime > 0)
            {
                fWaitTime -= (float)gameTime.ElapsedGameTime.Milliseconds;
                if (fWaitTime < 0)
                {
                    fWaitTime = 0;
                    change = true;
                }
            }

            Console.WriteLine("Wait Time: " + fWaitTime);
            Console.WriteLine("Elapsed  : " +  (float)gameTime.ElapsedGameTime.Milliseconds);
            sb.End();
            //base.Draw(gameTime);
        }
    }
}
